
package com.kedll.library.util;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.kedll.library.R;
import com.kedll.library.dialog.ECProgressDialogFragment;
import com.kedll.library.dialog.KBaseDialogFragment;

/**
 * @author Ring 日志类
 */
public class MyLog {
    private static StringBuffer logData = new StringBuffer();

    private static MyLog instance = new MyLog();

    public static MyLog getInstance() {
        return instance;
    }

//    private Dialog dialog;

    private KBaseDialogFragment dialog;
    private DialogInterface.OnDismissListener dismissListener;

    public void setDismissListener(DialogInterface.OnDismissListener listener) {
        getInstance().dismissListener = listener;
    }

    public void showProgressDialog(FragmentActivity activity, int msgId, DialogInterface.OnDismissListener dismissListener) {
        showProgressDialog(activity, activity.getString(msgId), dismissListener);
    }

    public void showProgressDialog(FragmentActivity activity, String msg, DialogInterface.OnDismissListener dismissListener) {

        if (dialog != null && dialog.isVisible()) {
            dialog.dismiss();
            dialog = null;
        }
        //            dialog = new ECProgressDialog(activity, msg);
        dialog = new ECProgressDialogFragment();
        dialog.setTitle(msg);
        getInstance().dismissListener = dismissListener;
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
                if (getInstance().dismissListener != null) {
                    getInstance().dismissListener.onDismiss(dialog);
                    getInstance().dismissListener = null;
                }
            }
        });
        dialog.show(activity.getSupportFragmentManager(), "progressDialog");
    }

    public void showProgressDialog(FragmentActivity activity, int msgId) {
        showProgressDialog(activity, activity.getString(msgId));
    }

    public void showProgressDialog(FragmentActivity activity, String msg) {
        showProgressDialog(activity, msg, null);
    }

    public void dismissDialog() {
        if (dialog == null || dialog.isHidden()) {
            return;
        }
        dialog.dismissAllowingStateLoss();
        dialog = null;
        if (dismissListener != null) {
            dismissListener = null;
        }
    }


    public static Dialog showPopupDialog(Context activity, View root) {
        final Dialog dialog = new Dialog(activity, R.style.my_dialog);
        dialog.setCanceledOnTouchOutside(true);

        dialog.setContentView(root);

        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.timepopwindow_anim_style);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = activity.getResources().getDisplayMetrics().widthPixels;
        window.setAttributes(lp);

        dialog.show();

        return dialog;
    }
}
